package com.attendance.chdingtask.controller;

import com.attendance.chdingtask.config.WebLog;
import com.attendance.chdingtask.model.*;
import com.attendance.chdingtask.service.MdAreaService;
import com.attendance.chdingtask.service.MdDetailsService;
import com.attendance.chdingtask.service.MdDeviceService;
import com.attendance.chdingtask.utils.common.Page;
import com.attendance.chdingtask.utils.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

@RestController
@Api(value = "md", description = "魔点数据相关接口", consumes = "application/json;charset=UTF-8")
@RequestMapping("/md")
public class MdDetailsController extends BaseController{

    @Autowired
    private MdDetailsService detailsService;

    @Autowired
    private MdAreaService areaService;

    @Autowired
    private MdDeviceService deviceService;


    @PostMapping("/findDetailsByData")
    @ResponseBody
    @WebLog(description = "详情首页数据明细")
    @ApiOperation(value = "魔点进出数据明细，根据条件查询", notes = "{\"userId\": \"用户id\",\"limit\": \"分页大小，此参数代表分页大小，最大100\",\"offset\": \"分页偏移，默认值：0\",\"startDate\": \"开始时间\",\"endDate\": \"结束时间\",\"searchDate\": \"搜索\"}")
    public Result findDetailsByData(HttpServletRequest request, @RequestBody UserQryParm vo ) {
        Page<MdDetails> page =new Page<>();
        try {
//            CorpUserDetail user = getUserInfo(request);
//            if(null ==user ){
//                return Result.error("用户不可为空，请检查！");
//            }
            checkNull(vo,"参数不可为空！");
            page.setPageCurrent(vo.getOffset());
            page.setPageSize(vo.getLimit());
            detailsService.findUserByData(vo,page,"dingfad484fb83b2303dffe93478753d9884");
//            checkNull(vo,"参数不可为空！");
//            page.setPageCurrent(vo.getOffset()+1);
//            page.setPageSize(vo.getLimit());
//            userInfoService.findUserByData(vo,page,null);

        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.success(page);
    }


    @PostMapping("/findAreasByData")
    @ResponseBody
    @WebLog(description = "地点统计数据")
    @ApiOperation(value = "地点统计数据，根据条件查询", notes = "{\"userId\": \"用户id\",\"limit\": \"分页大小，此参数代表分页大小，最大100\",\"offset\": \"分页偏移，默认值：0\",\"startDate\": \"开始时间\",\"endDate\": \"结束时间\",\"searchDate\": \"搜索\"}")
    public Result findUserByData(HttpServletRequest request, @RequestBody UserQryParm vo ) {
        Page<MdArea> page =new Page<>();
        try {
//            CorpUserDetail user = getUserInfo(request);
//            if(null ==user ){
//                return Result.error("用户不可为空，请检查！");
//            }
            if(null ==vo.getStartDate()){
                return Result.error("开始时间不能为空！");
            }
            if(null == vo.getEndDate()){
                return Result.error("结束时间不能为空！");
            }

            checkNull(vo,"参数不可为空！");
            page.setPageCurrent(vo.getOffset());
            page.setPageSize(vo.getLimit());
            areaService.findAreaByData(vo,page,"dingfad484fb83b2303dffe93478753d9884");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.success(page);
    }

    /**
     * 魔点数据合并导出excel
     * @param files
     * @return
     */
    @PostMapping("/exportData")
    @ResponseBody
    @WebLog(description = "魔点数据合并导出excel")
    @ApiOperation(value = "魔点数据合并导出excel,导出数据根据日期导出", notes = "{\"startDate\": \"开始时间\",\"endDate\": \"结束时间\"}")
    public Result exportData(@RequestBody UserQryParm vo, HttpServletResponse response) {
        try {

            if(null ==vo.getStartDate()){
                return Result.error("开始时间不能为空！");
            }
            if(null == vo.getEndDate()){
                vo.setEndDate(new Date());
            }
            // 告诉浏览器用什么软件可以打开此文件
            this.areaService.exportData(response,vo);

            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


//
//
//    @GetMapping("/exportData")
//    @ResponseBody
//    @WebLog(description = "魔点数据合并导出excel")
//    @ApiOperation(value = "魔点数据合并导出excel,导出数据根据日期导出", notes = "{\"startDate\": \"开始时间\",\"endDate\": \"结束时间\"}")
//    public Result exportData(UserQryParm vo) {
//        if(null ==vo.getStartDate()){
//            return Result.error("开始时间不能为空！");
//        }
//        if(null == vo.getEndDate()){
//            vo.setEndDate(new Date());
//        }
//        OutputStream out = null;
//        XSSFWorkbook wb = null;
//        try {
////            List<SchedulingExportVo> list = inspectionSchedulingExportService.exportScheduling(schedulingBo);
////            wb = inspectionSchedulingExportService.exportReceipt(list);
////            ExcelUtil<SchedulingExportVo> util = new ExcelUtil<SchedulingExportVo>(SchedulingExportVo.class);
////            String filename = util.encodingFilename("巡检排班");
////            out = new FileOutputStream(util.getAbsoluteFile(filename));
//            wb.write(out);
//            return Result.success();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return Result.error("导出Excel失败！");
//        } finally {
//            if (wb != null) {
//                try {
//                    wb.close();
//                } catch (IOException e1) {
//                    e1.printStackTrace();
//                }
//            }
//            if (out != null) {
//                try {
//                    out.close();
//                } catch (IOException e1) {
//                    e1.printStackTrace();
//                }
//            }
//        }
//    }


}
